package 蓝桥算法;

import 栈.LinkedListStack;

public class day13 {
    public boolean isValid(String s){//判断有效括号
        LinkedListStack<Character> a=new LinkedListStack<>(100);
        if(s.length()%2!=0){//长度不对
            return false;
        }
        char ch;
        for (int i = 0; i < s.length(); i++) {
            ch=s.charAt(i);//减少内存的浪费
            if(ch=='('){
                a.push(')');
            }
            else if(ch=='{'){
                a.push('}');
            }
            else if(ch=='['){
                a.push(']');
            }
            else if(a.isEmpty()||a.peek()!=ch){//不是上述的三种则应该弹栈匹配
                return false;
            }
            else {
                a.pop();//符合条件弹出
            }
        }
       return a.isEmpty();
    }
    public String removeDuplicates(String s){
        LinkedListStack<Character> stack=new LinkedListStack<>(100);
        char ch;
        for (int i = 0; i < s.length(); i++) {
            ch=s.charAt(i);
            if(stack.isEmpty()){
                stack.push(ch);
            }
            else if(stack.peek()==ch){
                stack.pop();
            }
            else if(stack.peek()!=ch){
                stack.push(ch);
            }
        }
        String str="";
        while(!stack.isEmpty()){
            str=stack.pop()+str;
        }
        return str;
    }
    public int evaLRPN(String s){
        LinkedListStack<Character> stack=new LinkedListStack<>(100);
        char ch;
        int z;
        for (int i = 0; i < s.length(); i++) {
            ch=s.charAt(i);
            if(ch=='+'||ch=='-'||ch=='*'||ch=='/'){
                int x=stack.pop();
                int y=stack.pop();
                if(ch=='+'){
                     z=x+y;
                }
                else if(ch=='-'){
                    z=x-y;
                }
                else if(ch=='*'){
                    z=x*y;
                }
                else if(ch=='/'){
                    z=x/y;
                }
                stack.push('z');
            }
        }
        return stack.pop();
    }

    public static void main(String[] args) {
        day13 b=new day13();
        String c="({[]})";
        String d="{[[[}]]";
        boolean y=b.isValid(c);
        boolean x=b.isValid(d);
        System.out.println(y);
        System.out.println(x);
        String z="adddsfgggf";
        String v=b.removeDuplicates(z);
        System.out.println(v);

    }
}
